function init() {
   checkLogin();
}

function checkLogin() {
   var map = new SimpleMap();
   map.put("op", XConstants.OP_CHECK_LOGIN);
   var request = new AdmRequest(map.writeBody(), checkLoginOver);
}

function checkLoginOver(req) {
   var rsps = new Response(req.responseText);

   if(rsps.getCode() == XConstants.SUCCESS) {
      $("loginDiv").innerHTML = "登录成功";
   }
   else if(rsps.getCode() == XConstants.ERROR) {
      $("loginDiv").className = "";
   }
}

function login() {
   var form = $("admLoginForm");
   var map = new SimpleMap();
   map.put("op", XConstants.OP_LOGIN);
   map.put("username", form.username.value);
   map.put("userpassword", form.userpassword.value);
   var request = new AdmRequest(map.writeBody(), loginOver, onLoading);
}

function loginOver(req) {
   var rsps = new Response(req.responseText);
   $("loadingDiv").className = "hidden";

   if(rsps.getCode() == XConstants.LOGIN_SUCCESS) {
      $("loginDiv").innerHTML = "登录成功";
   }
   else {
      showDialog('Error', '用户名密码错误，请重试', 'error', 1.5);
      $("loginDiv").className = "";
   }
}

function onLoading() {
   $("loadingDiv").className = "";
   $("loginDiv").className = "hidden";
}

function addNews() {
   var content = "<form action='#' onsubmit='return false;' id='newsForm'>" +
      "<table><tr><td>公告标题：</td>" +
      "<td><input type='text' name='newsTitle'/></td></tr>" +
      "<tr><td>公告内容：</td><td><textarea name='newsContent' cols='30' " +
      "rows='5' wrap='true'></textarea></td></tr>" +
      "</table><input type='button' value='确定' onclick='fireAddNews()'></form>";
   showDialog("添加公告", content, "prompt");
}

function fireAddNews() {
   var form = $("newsForm");
   var map = new SimpleMap();
   map.put("op", XConstants.OP_ADD_NEWS);
   hideDialog();

   for(var i = 0; i < form.elements.length; i++) {
      if(form.elements[i].name != null && form.elements[i].name != "") {
         map.put(form.elements[i].name, form.elements[i].value);
      }
   }

   var request = new AdmRequest(map.writeBody(), addNewsOver);
}

function addNewsOver(req) {
   var rsps = new Response(req.responseText);

   if(rsps.getCode() == XConstants.SUCCESS) {
      showDialog("Success", "添加成功", "success", 2);
   }
   else {
      showDialog("Error", "添加失败", "error", 2);
   }
}